<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncItemPath.php,v $
 * $Author: landseer $
 * $Revision: 1.3 $
 * $Date: 2004/08/01 07:27:17 $
 *
 * Type: Function
 *
 * creates a path string to the recet category, e.g. bigthings -> smaller things -> really small things
 *@param params['CategoryID'] int ID of the recent category
 *@param params['ItemName']   string items name
 *@param params['assign'] if set the an array of category information will be assign to this
                          variable
 *@param params['separator'] string string to separate the categories, efault is ">"
 *@param params['class']     string class name (css) for formatting the link, maybe empty
 *@return string path as seen in the example or assign the array to "assign"
 */
 
function smarty_function_pncItemPath($params, &$smarty)
{
    extract($params);
    unset($params);

    if( !$CategoryID )
    {
        $smarty->trigger_error( "pncItemPath: missing parameter CategoryID" );
        return false;
    }

    if( !$ItemName )
    {
        $smarty->trigger_error( "pncItemPath: missing parameter ItemName" );
        return false;
    }

    $class = (isset($class)) ? "class=\"$class\"" : "";
    $separator = (isset($separator)) ? $separator : ">";

    if( pnModAPILoad('pncommerce','user') )
    {
        // we need to remember the starting category id because we will re-use this variable now
        $startcategoryid = $CategoryID;
        // part one - read all categories and put them into an array
        $categorylist = array();
        while( $CategoryID)
        {
            // read th catgory information using the api function
            $category = pnModAPIFunc( 'pncommerce', 'user', 'getCategory', array( 'CategoryID' => $CategoryID ) );
            // replace CategoryID with ParentID
            $CategoryID = $category['ParentID'];
            // add this category in the beginning of the array, this result in an array
            // like [0] => first level [1] => second level [2] => third level
            array_unshift( $categorylist, $category );
        }
        // part two - see what the user wants
        // if assign is set -> simply assign the categorylist to $assign
        // if not, create the string as usual
        if( isset($assign) )
        {
            $smarty->assign($assign, $categorylist );
            return;
        }
        else
        {
            // now the work starts
            // add "Start" at the beginning to point to the catalogview
            $url = pnModAPIFunc( 'pncommerce', 'user', 'pncModURL',
                                 array( 'Module' => 'pncommerce',
                                        'Section' => 'user',
                                        'Function' => 'catalogview' ) );
            $cattree = "<a $class href='".pnVarPrepForDisplay($url)."'>".pnVarPrepForDisplay(_PNC_START)."</a> $separator ";
            foreach( $categorylist as $categoryentry )
            {
                if( $categoryentry['Invisible'] == 1 )
                {
                    break;
                } 

                // add a link to the category
                $url = pnModAPIFunc( 'pncommerce', 'user', 'pncModURL',
                                     array( 'Module' => 'pncommerce',
                                            'Section' => 'user',
                                            'Function' => 'categoryview',
                                            'Args' => array( 'CategoryID' => $categoryentry['ID'] ) ) );
                $cattree .= "<a $class href='$url'>".$categoryentry['Name']."</a> $separator ";

                if( $categoryentry['ID'] == $startcategoryid )
                {
                    // we reached the starting category (the one recently shown) so we simply 
                    // show its name here instead of showing a redundant link
                    $cattree .= $ItemName;
                }
            }
            return $cattree;
        }
    }
    else
    {
        $smarty->trigger_error( "pncCategoryPath: cannot load pnuserapi" );
        return false;
    }
}

?>
